<?php
/**
 * @since      File available since Release v1.1
 */
defined('Inbearshop') or exit('Access Invalid!');
define('MYSQL_RESULT_TYPE',1);
class renzhengControl extends BaseHomeControl{
	public function __construct(){
		parent::__construct();
        Language::read('indexs');
        Tpl::output('index_sign','index');
	}

	
	/*
	 * 资质认证
	 * 雪饮
	 */
	public function renzhengOp(){
		
		$model = Model();
		
		$setting = $model->table('setting')->where(array('name'=>'shop_cash'))->find();
		Tpl::output('cash',$setting['value']);
		
		$user = $model->table('user')->where(array('user_id'=>$_COOKIE['user_id']))->find();
		Tpl::output('user',$user);
		
		$company = $model->table('company')->where(array('user_id'=>$_COOKIE['user_id']))->find();
		Tpl::output('company',$company);

		Tpl::showpage('my.renzheng');
	}
	
	/*
	 * 资质认证
	 * 小熊Shop
	 */
	public function renzheng1Op(){
		$model = Model();
	
		$user = $model->table('user')->where(array('user_id'=>$_COOKIE['user_id']))->find();
		Tpl::output('user',$user);
		
		$company = $model->table('company')->where(array('user_id'=>$_COOKIE['user_id']))->find();
		Tpl::output('company',$company);
		
		if($_GET['type']=='person'){
			Tpl::showpage('my.renzheng.person');
		}elseif($_GET['type']=='business'){
			Tpl::showpage('my.renzheng.business');
		}
		exit;	
	}
	public function zhuanchangOp(){
	    $model = Model();
        $user = $model->table('user')->where(array('user_id'=>$_COOKIE['user_id']))->find();
        $canjia_zc_id=$model->table('zc_shenqing')->where(array('user_id'=>$_COOKIE['user_id']))->field('zc_id')->select();
        $canjia_zc_id_str="";
        if(!empty($canjia_zc_id)){
            foreach($canjia_zc_id as $key=>$val){
                if($key==count($canjia_zc_id)-1){$canjia_zc_id_str.=$val['zc_id'];}
                else{$canjia_zc_id_str.=$val['zc_id'].",";}
            }
        }
        $map['id'] = array('not in',$canjia_zc_id_str);
        $map['end_time']=array('gt',time());
        Tpl::output('user',$user);
        $company = $model->table('special')->where($map)->order('id desc')->page('10')->select();
        foreach ($company as $k => $v) {
            if (time() < $company[$k]["start_time"]) {
                $company[$k]['status'] = 1;
            } else {
                $company[$k]['status'] = 0;
            }
        }
        Tpl::output('list', $company);
        Tpl::output('show_page', $model->showpage());
        //Tpl::showpage('my.zhuanchang');
        Tpl::showpage('zsm/my.zhuanchang');
    }

    public function zhuanchang_ztOp()
    {
        $model = Model();
        $canjia_zc_id = $model->table('zc_shenqing')->where(array('user_id' => $_COOKIE['user_id']))->select();
        $canjia_zc_id_str = "";
        if (!empty($canjia_zc_id)) {
            foreach ($canjia_zc_id as $key => $val) {
                if ($key == count($canjia_zc_id) - 1) {
                    $canjia_zc_id_str .= $val['zc_id'];
                } else {
                    $canjia_zc_id_str .= $val['zc_id'] . ",";
                }
            }
        }
        $map['id'] = array('in', $canjia_zc_id_str);
        $company = $model->table('special')->where($map)->order('id desc')->page('10')->select();
        foreach ($company as $k => $v) {
            if (time() < $company[$k]["start_time"]) {
                $company[$k]['status'] = 1;
            } else {
                $company[$k]['status'] = 0;
            }
            foreach ($canjia_zc_id as $key => $val) {
                if ($canjia_zc_id[$key]["zc_id"] == $company[$k]["id"]) {
                    $company[$k]["sq_zt"] = $canjia_zc_id[$key]["status"];
                    $company[$k]["sq_id"] = $canjia_zc_id[$key]["id"];
                    $company[$k]["bohui_liyou"] = $canjia_zc_id[$key]["liyou"];
                }
            }
        }
        Tpl::output('list', $company);
        Tpl::output('user_id', $_COOKIE['user_id']);
        Tpl::output('show_page', $model->showpage());
        //Tpl::showpage('my.zhuanchang2');
        Tpl::showpage('zsm/my.zhuanchang2');
    }

    public function zhuanchang_jlOp()
    {
        $model = Model();
        $condition['user_id'] = $_COOKIE['user_id'];
        $condition['status'] = 2;
        $res = $model->table('zc_shenqing')->where($condition)->select();
        $condition_id_arr = array();
        foreach ($res as $key => $val) {
            array_push($condition_id_arr, $res[$key]["zc_id"]);
        }
        $condition2['id'] = array('in', $condition_id_arr);
        $condition2['end_time'] = array('lt', time());
        $res2 = $model->table('special')->where($condition2)->order('id desc')->page('10')->select();
        Tpl::output('list', $res2);
        Tpl::output('show_page', $model->showpage());
        Tpl::showpage('my.zhuanchang_jl');
    }

    public function zhuanchang_paipinOp()
    {
        $model = Model();
        $user = $model->table('user')->where(array('user_id' => $_COOKIE['user_id']))->find();
        $canjia_zc_id = $model->table('zc_shenqing')->where(array('user_id' => $_COOKIE['user_id'], 'status' => 2))->select();
        $canjia_zc_id_str = "";
        if (!empty($canjia_zc_id)) {
            foreach ($canjia_zc_id as $key => $val) {
                if ($key == count($canjia_zc_id) - 1) {
                    $canjia_zc_id_str .= $val['zc_id'];
                } else {
                    $canjia_zc_id_str .= $val['zc_id'] . ",";
                }
            }
        }
        $map['id'] = array('in', $canjia_zc_id_str);
        $map['end_time'] = array('gt', time());
        Tpl::output('user', $user);
        $company = $model->table('special')->where($map)->order('id desc')->page('10')->select();
        foreach ($company as $k => $v) {
            if (time() < $company[$k]["start_time"]) {
                $company[$k]['status'] = 1;
            } else {
                $company[$k]['status'] = 0;
            }
            foreach ($canjia_zc_id as $key => $val) {
                if ($canjia_zc_id[$key]["zc_id"] == $company[$k]["id"]) {
                    $company[$k]["sq_zt"] = $canjia_zc_id[$key]["status"];
                    $company[$k]["sq_id"] = $canjia_zc_id[$key]["id"];
                    $company[$k]["bohui_liyou"] = $canjia_zc_id[$key]["liyou"];
                }
            }
        }
        Tpl::output('list', $company);
        Tpl::output('show_page', $model->showpage());
        //Tpl::showpage('my.zhuanchang3');
        Tpl::showpage('zsm/my.zhuanchang3');
    }

    public function zhuanchang_paipin_ckOp()
    {
        $model = Model();
        $map['seller_id'] = $_COOKIE['user_id'];
        $map['Relation_zc_id'] = array('gt', 0);
        $auction = $model->table('auction')->where($map)->page('20')->select();
        foreach ($auction as $key => $val) {
            $ppimgs = $model->table('auction_img')->where(array('auction_id' => $auction[$key]['id']))->select();
            $auction[$key]['imgs'] = $ppimgs;
            $auction_zcid = $auction[$key]['Relation_zc_id'];
            $zcfind = $model->table('special')->where(array('id' => $auction_zcid))->find();
            $auction[$key]['zc_name'] = $zcfind['name'];
        }
        Tpl::output('list', $auction);
        Tpl::output('show_page', $model->showpage());
        //Tpl::showpage('my.zhuanchang4');
        Tpl::showpage('zsm/my.zhuanchang4');
    }

    public function del_auctionOp()
    {
        $model = Model();
        $auction_id = $_GET['auction_id'];
        $auction_find = $model->table('auction')->where(array('id' => $auction_id))->find();
        $auction_video = $auction_find['auction_video'];
        unlink($_SERVER['DOCUMENT_ROOT'] . $auction_video);
        $auction_img_select = $model->table('auction_img')->where(array('auction_id' => $auction_id))->select();
        foreach ($auction_img_select as $key => $val) {
            unlink($_SERVER['DOCUMENT_ROOT'] . "/bearshop/data/upload/shop/auction/" . $auction_find['auction_add_date'] . "/" . $val['auction_img']);
        }
        $model->table('auction')->where(array('id' => $auction_id))->delete();
        $model->table('auction_img')->where(array('auction_id' => $auction_id))->delete();
        echo "删除成功！！！<a href='javascript:window.history.go(-1);'>点击返回</a>";
    }
	
	
	public function steppersonOp(){
		$model = Model();
		
		if(!empty($_POST)){
			$params = array();
			$params['user_id'] = $_COOKIE['user_id'];
			$params['company_type'] = '0';			
			$params['company_contract'] = trim($_POST['name']);
			$params['company_cardid'] = trim($_POST['idCode']);
			$params['company_mobile'] = trim($_POST['tel']);
			$params['company_state'] = 1;
			$params['company_code_pic1'] = trim($_POST['idcard1img']);
			$params['company_code_pic2'] = trim($_POST['idcard2img']);
			$params['company_code_pic3'] = trim($_POST['idcard3img']);
			$params['refusereason'] = '';
				
			$company = $model->table('company')->where(array('user_id'=>$_COOKIE['user_id']))->find();
			if(!empty($company)){
				$res = $model->table('company')->where(array('user_id'=>$_COOKIE['user_id']))->update($params);
			}else{
				$res = $model->table('company')->insert($params);
			}

			if($res){
				echo json_encode(array('state'=>1));
			}else{
				echo json_encode(array('state'=>0));
			}
			exit;
		}
		
		$user = $model->table('user')->where(array('user_id'=>$_COOKIE['user_id']))->find();
		Tpl::output('user',$user);
		Tpl::showpage('my.step.person');
	}
	
	public function stepbusinessOp(){
		$model = Model();
	
		if(!empty($_POST)){
			$params = array();
			$params['user_id'] = $_COOKIE['user_id'];
			$params['company_type'] = '1';
			$params['company_contract'] = trim($_POST['name']);
			$params['company_cardid'] = trim($_POST['idCode']);
			$params['company_mobile'] = trim($_POST['tel']);
			$params['company_state'] = 1;
			$params['company_name'] = trim($_POST['company_name']);
			$params['company_code_pic1'] = trim($_POST['idcard1img']);
			$params['company_code_pic2'] = trim($_POST['idcard2img']);
			$params['company_code_pic3'] = trim($_POST['idcard3img']);
			$params['company_code_pic4'] = trim($_POST['idcard4img']);
			$params['refusereason'] = '';
			
			$company = $model->table('company')->where(array('user_id'=>$_COOKIE['user_id']))->find();
			if(!empty($company)){
				$res = $model->table('company')->where(array('user_id'=>$_COOKIE['user_id']))->update($params);
			}else{
				$res = $model->table('company')->insert($params);
			}
			
	
			if($res){
				echo json_encode(array('state'=>1));
			}else{
				echo json_encode(array('state'=>0));
			}
			exit;
		}
	
		$user = $model->table('user')->where(array('user_id'=>$_COOKIE['user_id']))->find();
		Tpl::output('user',$user);
		Tpl::showpage('my.step.business');
	}
	
	
	public function stepperson1Op(){
		
		
		Tpl::showpage('stepperson1');
	}
	
	public function uploadpicOp(){
		$serverId = $_POST['serverId'];

		if(!empty($serverId)){
			$filename = $this->downloadpic($serverId);
			echo json_encode(array('state'=>1,'filename'=>$filename));
			exit;
		}
		
	}
	
	
	public function downloadpic($mediaid){
		$access_token = $this->getAccessTokenApi();
		$url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$mediaid}";
		$fileInfo = $this->downloadWeixinFile($url);
		$array = json_decode($fileInfo["body"],true);

		if(!empty($array['errcode'])){
			$access_token = $this->getAccessToken();
			$url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$mediaid}";
			$fileInfo = $this->downloadWeixinFile($url);
		}
		
		$filename = "./data/upload/shop/zhizhao/{$mediaid}.jpg";
		$this->saveWeixinFile($filename, $fileInfo["body"]);
		return "{$mediaid}.jpg";
	}
	
	
	private function saveWeixinFile($filename, $filecontent) {
		$local_file = fopen($filename, 'w');
		if (false !== $local_file){
			if (false !== fwrite($local_file, $filecontent)) {
				fclose($local_file);
			}
		}
	}
	
	
	
	private function downloadWeixinFile($url) {
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_NOBODY, 0); //只取body头
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$package = curl_exec($ch);
		$httpinfo = curl_getinfo($ch);
		curl_close($ch);
		$imageAll = array_merge(array('header' => $httpinfo), array('body' => $package));
		return $imageAll;
	}
}